/**
 * @author NGUYEN Van Tho
 */

package ifi.p17.view;

import ifi.p17.Constant;
import ifi.p17.Person;
import ifi.p17.controler.Control;
import ifi.p17.layout.LayoutHelper;
import ifi.p17.model.Model;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class ViewAddNewPerson extends View implements ActionListener{

//	private static final int NUMBER_OF_FIELDS = 5;
	private static final String LABEL_SAVE = "Enregistrer";
	private static final String LABEL_CANCEL = "Annuler";
	private static final String MESSAGE_SAVE_SUCCESFUL = "Une personne a été enregistrée";
	private static final String MESSAGE_SAVE_ERROR = "Un erreur";
    private static final String s = "Click a button.";
    //private Model model;
    private Control control;
    
    private JLabel labelPersonName 			= new JLabel("Name", JLabel.LEFT);
    private JLabel labelPersonHomePhone 	= new JLabel("Home phone", JLabel.LEFT);
    private JLabel labelPersonWorkPhone 	= new JLabel("Work phone", JLabel.LEFT);
    private JLabel labelPersonHomeAddress 	= new JLabel("Home Address", JLabel.LEFT);
    private JLabel labelPersonWorkAddress 	= new JLabel("Work Address", JLabel.LEFT);
      
    private JTextField textPersonName 			= new JTextField(100);
    private JTextField textPersonHomePhone 		= new JTextField(100);
    private JTextField textPersonWorkPhone 		= new JTextField(100);
    private JTextField textPersonHomeAddress 	= new JTextField(100);
    private JTextField textPersonWorkAddress 	= new JTextField(100);
    
    public ViewAddNewPerson(Model model) {
        super(new GridBagLayout());        
        
        LayoutHelper layoutHelper = new LayoutHelper();
        layoutHelper.addLabel(labelPersonName, this);
        layoutHelper.addTextField(textPersonName, this);
        layoutHelper.addLabel(labelPersonHomePhone, this);
        layoutHelper.addTextField(textPersonHomePhone, this);
        layoutHelper.addLabel(labelPersonWorkPhone, this);
        layoutHelper.addTextField(textPersonWorkPhone, this);
        layoutHelper.addLabel(labelPersonHomeAddress, this);
        layoutHelper.addTextField(textPersonHomeAddress, this);
        layoutHelper.addLabel(labelPersonWorkAddress, this);
        layoutHelper.addTextField(textPersonWorkAddress, this);
              
        JPanel panelButton = new JPanel();
        JButton buttonSave = new JButton(LABEL_SAVE);
        JButton buttonCancel = new JButton(LABEL_CANCEL);
        buttonSave.addActionListener(this);
        buttonCancel.addActionListener(this);
        panelButton.add(buttonSave);
        panelButton.add(buttonCancel);
        
        layoutHelper.addPanel(panelButton, this);
        
        //model.addObserver(new ModelObserver());
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    	String command = e.getActionCommand();
    	if (LABEL_SAVE.equals(command)) {
    		Person person = new Person();
    		person.setName(textPersonName.getText());
    		person.setHomeTelephone(textPersonHomePhone.getText());
    		person.setWorkTelephone(textPersonWorkPhone.getText());
    		person.setHomeAddress(textPersonHomeAddress.getText());
    		person.setWorkAddress(textPersonWorkAddress.getText());
    		//boolean result = model.insert(person);
    		
    		boolean result = control.insertPerson(person);
    		if(result){
    			//JDialog dialog = new JDialog(SwingUtilities.getWindowAncestor(this), "Vous");
    			//JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this), "Vous");
    			//JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this), MESSAGE_SAVE_SUCCESFUL);
//    			JOptionPane pane = new JOptionPane();
//    			JDialog dialog = pane.createDialog(SwingUtilities.getWindowAncestor(this), MESSAGE_SAVE_SUCCESFUL);
//    			dialog.show();
//    		    Object selectedValue = pane.getValue();
//    			System.out.println(selectedValue);
//    			int res = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this), MESSAGE_SAVE_SUCCESFUL, "", JOptionPane.OK_OPTION);
//    		  //  DataDialog dialog = new DataDialog(SwingUtilities.getWindowAncestor(this), false, MESSAGE_SAVE_SUCCESFUL);
//    			System.out.println(res);
//    			
//    			if(res == 0){
//    				control.changeView(MainView.LIST_VIEW);
//    			}
    			control.changeView(MainView.LIST_VIEW);
    			
    		}
//    		textPersonWorkPhone.setText(textPersonHomePhone.getText());
			
    	}
    	if (LABEL_CANCEL.equals(command)) {
    		SwingUtilities.getWindowAncestor(this).setVisible(false);
    		SwingUtilities.getWindowAncestor(this).dispose();
    	}
    }
    
    public void clear(){
		textPersonName.setText("");
		textPersonHomePhone.setText("");
		textPersonWorkPhone.setText("");
		textPersonHomeAddress.setText("");
		textPersonWorkAddress.setText("");    	
    }
    public void setController(Control control){
    	this.control = control;
    }
    
}